<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of browser
 *
 * @author Andriy
 */
class Browser
{
    protected $_proxy = null;
    
    protected $_cookie = null;
    
    protected $_ajax_header = array(
        'Host' => 'digg.com',
        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0',
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',//'application/json, text/javascript, */*; q=0.01',
        'Accept-Language' => 'uk,ru;q=0.8,en-us;q=0.5,en;q=0.3',
        'Accept-Encoding' => 'gzip, deflate',
        'Accept-Charset' => 'windows-1251,utf-8;q=0.7,*;q=0.7',
        'Keep-Alive' => '115',
        'Connection' =>	'keep-alive',
        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
        'X-Requested-With' => 'XMLHttpRequest',
        'Referer' => 'http://westua.net/',
        'Content-Length' => '200',
        'Pragma' => 'no-cache',
        'Cache-Control' => 'no-cache'
    );


    public function get( $url,  $post_data = null, $is_ajax = false )
    {
//        $url = str_replace( "&amp;", "&", urldecode(trim($url)) );
        $ch = curl_init();
//        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16" );
        curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30" );
        curl_setopt( $ch, CURLOPT_URL, $url );
        
        if( !$this->_cookie )
        {
            $this->_cookie = tempnam ("tmp", "CURLCOOKIE");
        }
        curl_setopt( $ch, CURLOPT_COOKIEJAR, $this->_cookie );
        curl_setopt( $ch, CURLOPT_COOKIEFILE, $this->_cookie );
        
        curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $ch, CURLOPT_ENCODING, "" );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
        
        if( $post_data )
        {
            curl_setopt($ch, CURLOPT_POST, 1); 
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
        }
        
        if( $is_ajax )
        {
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->_ajax_header);
        }
        
        if( $this->_proxy )
        {
            curl_setopt( $ch, CURLOPT_HTTPPROXYTUNNEL, 1 );
            curl_setopt( $ch, CURLOPT_PROXY, $this->_proxy->getProxy() ); 
        }
        
        $content = curl_exec( $ch );
//        $status = curl_getinfo($ch,CURLINFO_HTTP_CODE); 
//        print_r($status); echo "<br />";
        curl_close ( $ch );
        
        return $content;
    }
    
    public function setProxy( Proxy $proxy )
    {
        $this->_proxy = $proxy;
    }
    
}

?>
